#include <iostream>
#include <cmath>
#include <fstream>
#include <chrono>
#include <algorithm>

using namespace std;

void bubble_sort(int nums[], int size) {

    for (int i = 0; i < size - 1; ++i) {
        bool swapped = false;
        for (int j = 0; j < size - 1 - i; ++j) {
            if (nums[j] > nums[j + 1]) {
                swap(nums[j], nums[j + 1]);
                swapped = true;
            }
        }
        if (!swapped) {
            break;
        }
    }
}

void read_to_array(const string& filename, int nums[], int size) {
    ifstream file(filename);
    for (int i = 0; i < size; ++i){
        file >> nums[i];
    }
}

int main() {
    const int SIZE = 5000;
    int nums[SIZE];

    const string filenames[] = {"m1_sorted.txt", "m2_reverse.txt", "m3_shuffle.txt"};

    for (const auto& filename : filenames) {
        read_to_array(filename, nums, SIZE);
        auto start_time = chrono::high_resolution_clock::now();
        bubble_sort(nums, SIZE);
        auto end_time = chrono::high_resolution_clock::now();
        chrono::duration<double, milli> elapsed_time = end_time - start_time;
        cout << filename.substr(0, filename.find('.')) << " " << round(elapsed_time.count()) << " milliseconds" << endl;
      }

    return 0;
}
